package com.his.common.util;

import com.his.sysconfig.entity.Sysconfig;
import com.his.sysconfig.service.SysconfigService;
import com.xiao.common.util.RedisUtils;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author xiaoxiaoping
 * @Date 2020/3/26 14:06
 * @Version 1.0
 */
@Component
public class SerialNoUtil {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private SysconfigService sysconfigService;

    public Integer getSerialNo(String dateStr ,String key){
        key = dateStr+"_"+key;
        redisUtils.incr(key,1l);
        if(redisUtils.getExpire(key)<0){
            redisUtils.expire(key,7*24*3600);
        }
        return (Integer) redisUtils.get(key);
    }

    /**
     * 新获取门诊号
     * @param hospId
     * @return
     */
    public Integer getOutPatiId(String hospId){
        String key = "out_pati_id_"+hospId;
        if(redisUtils.hasKey(key)){
            redisUtils.incr(key,1);
            return (Integer) redisUtils.get(key);
        }else{
            Sysconfig sysconfig = new Sysconfig();
            sysconfig.setHospid(hospId);
            sysconfig.setSysconfigtypeid("0203");
            sysconfig.setSysconfigid("02031D");
            String outPatiId = sysconfigService.getSysConfigValue(sysconfig);
            redisUtils.set(key,Integer.valueOf(outPatiId));
            return Integer.valueOf(outPatiId);
        }
    }

    /**
     * 获取新的住院号
     * @param hospId
     * @return
     */
    public Integer getInPatiId(String hospId){
        String key = "in_pati_id_"+hospId;
        if(redisUtils.hasKey(key)){
            redisUtils.incr(key,1);
            return (Integer) redisUtils.get(key);
        }else{
            Sysconfig sysconfig = new Sysconfig();
            sysconfig.setHospid(hospId);
            sysconfig.setSysconfigtypeid("0202");
            sysconfig.setSysconfigid("020206");
            String outPatiId = sysconfigService.getSysConfigValue(sysconfig);
            redisUtils.set(key,Integer.valueOf(outPatiId));
            return Integer.valueOf(outPatiId);
        }
    }
}
